19. CPU Stats
printCpuStats()
11# Class ProcessParser - PrintCpuStats
This method calculates CPU usage, either overall or for a selected core. The function has two parameters: previous and current time.
CPU stats are time-dependent, so the only way to get valid CPU statistics are by specifying a time interval.
string ProcessParser::printCpuStats(vector<string> values1, vector<string> values2)
{
/*
Because CPU stats can be calculated only if you take measures in two different time,
this function has two parameters: two vectors of relevant values.
We use a formula to calculate overall activity of processor.
*/
float activeTime = getSysActiveCpuTime(values2) - getSysActiveCpuTime(values1);
float idleTime = getSysIdleCpuTime(values2) - getSysIdleCpuTime(values1);
float totalTime = activeTime + idleTime;
float result = 100.0*(activeTime / totalTime);
return to_string(result);
}